/****************************************************************************
 * @file    FloraIniManager.cpp
 * @brief   Implementation of the FloraIniManager class.
 * @details This file contains the implementation of the FloraIniManager class,
 *          which provides methods to read and write configuration values
 *          from and to an INI file.
 *
 * @author  繁花遁
 * @date    2024-08-30
 ****************************************************************************/

#include "FloraIniManager.h"
#include <QPushButton>
#include <QCheckBox>
#include <QLineEdit>
#include <QSpinBox>
#include <QComboBox>
#include <QRadioButton>
#include <QTextEdit>
#include <QSlider>
// Constructor
FloraIniManager::FloraIniManager(const QString &filePath) : settings(filePath, QSettings::IniFormat) {}

// Get methods
bool FloraIniManager::getBool(const QString &section, const QString &key) {
    return settings.value(section + "/" + key).toBool();
}

int FloraIniManager::getInt(const QString &section, const QString &key) {
    return settings.value(section + "/" + key).toInt();
}

QString FloraIniManager::getString(const QString &section, const QString &key) {
    return settings.value(section + "/" + key).toString();
}

QStringList FloraIniManager::getStringList(const QString &section, const QString &key) {
    return settings.value(section + "/" + key).toStringList();
}

// Set methods
void FloraIniManager::setValue(const QString &section, const QString &key, bool value) {
    settings.setValue(section + "/" + key, value);
}

void FloraIniManager::setValue(const QString &section, const QString &key, int value) {
    settings.setValue(section + "/" + key, value);
}

void FloraIniManager::setValue(const QString &section, const QString &key, const QString &value) {
    settings.setValue(section + "/" + key, value);
}

void FloraIniManager::setValue(const QString &section, const QString &key, const QStringList &value) {
    settings.setValue(section + "/" + key, value);
}

void FloraIniManager::saveLintEdit( QString section, QLineEdit *le)
{
    QString key = le->objectName();
    settings.setValue(section + "/" + key, le->text());
}

QString FloraIniManager::getLe( QString section, QLineEdit *le)
{
    QString key = le->objectName();
    QString value = settings.value(section + "/" + key).toString();
    le->setText(value);
    return value;

}

void FloraIniManager::saveWidget(QString section,QWidget *widget)
{

    if (QCheckBox *checkbox = qobject_cast<QCheckBox*>(widget)) {
        QString key = checkbox->objectName();
        settings.setValue(section + "/" + key, checkbox->isChecked());
    }
    else if (QLineEdit *lineEdit = qobject_cast<QLineEdit*>(widget)) {
        QString key = lineEdit->objectName();
        settings.setValue(section + "/" + key, lineEdit->text());
    }
    else if (QSpinBox *spinBox = qobject_cast<QSpinBox*>(widget)) {
        QString key = spinBox->objectName();
        settings.setValue(section + "/" + key, spinBox->value());
    }
    else if (QComboBox *comboBox = qobject_cast<QComboBox*>(widget)) {
        QString key = comboBox->objectName();
        settings.setValue(section + "/" + key, comboBox->currentText());
    }
    else if (QRadioButton *radioButton = qobject_cast<QRadioButton*>(widget)) {
        QString key = radioButton->objectName();
        settings.setValue(section + "/" + key, radioButton->isChecked());
    }
    else if (QTextEdit *textEdit = qobject_cast<QTextEdit*>(widget)) {
        QString key = textEdit->objectName();
        settings.setValue(section + "/" + key, textEdit->toPlainText());
    }
    else if (QPushButton *pushButton = qobject_cast<QPushButton*>(widget)) {
        QString key = pushButton->objectName();     
        settings.setValue(section + "/" + key, pushButton->isChecked());
    }
    else if (QSlider *slider = qobject_cast<QSlider*>(widget)) {
        QString key = slider->objectName();
        settings.setValue(section + "/" + key, slider->value());
    }         
}

void FloraIniManager::loadWidget(QString section,QWidget *widget)
{
    if (QCheckBox *checkbox = qobject_cast<QCheckBox*>(widget)) {
        QString key = checkbox->objectName();
        checkbox->setChecked(settings.value(section + "/" + key).toBool());
    }
    else if (QLineEdit *lineEdit = qobject_cast<QLineEdit*>(widget)) {
        QString key = lineEdit->objectName();
        lineEdit->setText(settings.value(section + "/" + key).toString());
    }
    else if (QSpinBox *spinBox = qobject_cast<QSpinBox*>(widget)) {
        QString key = spinBox->objectName();    
        spinBox->setValue(settings.value(section + "/" + key).toInt());
    }
    else if (QComboBox *comboBox = qobject_cast<QComboBox*>(widget)) {
        QString key = comboBox->objectName();
        comboBox->setCurrentText(settings.value(section + "/" + key).toString());
    }   
    else if (QRadioButton *radioButton = qobject_cast<QRadioButton*>(widget)) {
        QString key = radioButton->objectName();
        radioButton->setChecked(settings.value(section + "/" + key).toBool());
    }
    else if (QTextEdit *textEdit = qobject_cast<QTextEdit*>(widget)) {  
        QString key = textEdit->objectName();
        textEdit->setPlainText(settings.value(section + "/" + key).toString());
    }
    else if (QPushButton *pushButton = qobject_cast<QPushButton*>(widget)) {
        QString key = pushButton->objectName();
        pushButton->setChecked(settings.value(section + "/" + key).toBool());        
    }
    else if (QSlider *slider = qobject_cast<QSlider*>(widget)) {
        QString key = slider->objectName();
        slider->setValue(settings.value(section + "/" + key).toInt());
    }   
}



